var layer;
var form;
var table;
var lay;
var tempConfig;

layui.use(['layer', 'form', 'table'], function () {
    layer = layui.layer;
    form = layui.form;
    table = layui.table;

    // 执行一个 table 实例=============================================================================================
    table.render({
        elem: '#data_table',
        id: 'data_table',
        url: ctxPath + 'generator/selectTablePage',
        title: '',
        //height: 'full-120',
        //size:"sm",
        page: true,
        toolbar: '#toolberLeft',
        cols: [[
            {type : 'checkbox', fixed : 'left'},
            {field: 'tableName', title: '表名称', minWidth: '200', hide: setColumnHide("tableName")},
            {field: 'tableComment', title: '注释', minWidth: '200', hide: setColumnHide("tableComment")},
            {field: 'engine', title: '表类型', minWidth: '200', hide: setColumnHide("engine")},
            {field : 'createTime', title : '创建时间', align : 'center', minWidth: '200', hide: setColumnHide("createTime")},
            {fixed: isRight(), align: 'center', minWidth: '200', title: '操作', toolbar: '#toolRow'}
        ]]
    });

    // 头部左侧事件=============================================================================================
    table.on('toolbar(data_table)', function (obj) {
        if (obj.event === 'add') {
            var names = getTableCheckIds("data_table", "tableName");
            if(names){
                var tableComments = getTableCheckIds("data_table", "tableComment");
                var s = '<span class="all-icon layui-layer-save-icon layui-layer-save-col-cancel" style="font-size: 30px;">&#xe643;</span>';
                s += '<span class="layui-layer-save-tips" style="font-size: 14px;color: #524f4f;">确认要将选中的行生成代码吗？</span>';
                layer.confirm(s, {
                    title: "提示",
                    time: 0,
                    closeBtn: 0,
                    shade: 0.1,
                    offset: '20%',
                    success: function (layero) {
                        $(layero).css({'border':'0px', 'box-shadow':'#adafaf 0px 0px 50px 0px'});
                    }
                }, function (index) {
                    getAjax(ctxPath + 'generator/createCodeTables', {"tableNames": names, "tableComments": tableComments}, 'POST', function (res) {
                        showTips(res.msg);
                    })
                    layer.close(index);
                });
            }else {
                layer.msg("请至少选择一行", {offset: "20%"})
            }
        }
    })

    // 行监听事件=============================================================================================
    table.on('tool(data_table)', function (obj) {
        var data = obj.data;
        if (obj.event === 'edit') {
            var title = '<i class="layui-icon" style="color: #ff7600;font-size: 20px;">&#xe702;</i>';
            title += '<span style="margin-left: 5px;position: relative;top: -2px;">勾选 <b>不需要</b> 的数据列，进行代码生成</span>';
            lay = openWindow('field_form', title, '90%', '90%', false, 0, false, false, data, 0.2, false, function () {
                // 执行一个 table 实例=============================================================================================
                table.render({
                    elem: '#field_data_table',
                    id: 'field_data_table',
                    url: ctxPath + 'generator/selectTableFieldPage'+"?name="+data.tableName,
                    title: '',
                    height: 'full-230',
                    page: false,
                    toolbar: false,
                    cols: [[
                        {type : 'checkbox', fixed : 'left'},
                        {field: 'columnName', title: '字段名称', minWidth: '200', hide: setColumnHide("columnName")},
                        {field: 'columnType', title: '字段类型', minWidth: '200', hide: setColumnHide("columnType")},
                        {field: 'columnComment', title: '字段备注', minWidth: '200', hide: setColumnHide("columnComment")},
                    ]]
                    , done: function (result, currPage, count) {
                        var pageList = result.data;
                        var array = tempConfig.notField.split(",");
                        for(var i = 0; i < pageList.length; i++){
                            for (var j = 0; j <array.length; j++){
                                if(pageList[i].columnName == array[j]){
                                    //$('div[lay-id="field_data_table"] .layui-table-body tr[data-index='+i+'] .layui-form-checkbox').remove();
                                    //$('div[lay-id="field_data_table"] .layui-table-body tr[data-index='+i+'] input[type=checkbox]').remove();
                                    $('div[lay-id="field_data_table"] .layui-table-fixed tr[data-index='+i+'] .layui-form-checkbox').click();
                                    break;
                                }
                            }
                        }
                        form.render('checkbox');
                    }
                });
                var tableName = data.tableName;
                var tableComment = data.tableComment;
                // 头部左侧事件=============================================================================================
                $("#add_one").click(function(){
                    var names = getTableCheckIds("field_data_table", "columnName");
                    if(names){
                        getAjax(ctxPath + 'generator/createOneCode', {"fieldNames": names, "tableNames": tableName, "tableComments": tableComment}, 'POST', function (res) {
                            showTips(res.msg);
                            if(res.success){
                                layer.close(lay);
                            }
                        })
                    }else {
                        layer.msg("请至少选择一行", {offset: "20%"})
                    }
                })
            })
        }
    });

    getConfig();



})



/**
 * --------------------------------------------------------------------------------------------------------------------------
 * 获取数据配置
 */
function getConfig() {
    $.get(ctxPath + 'generator/selectOne', {}, function (result) {
        tempConfig = result.data;
    })
}
